/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.antColonyOptimisation.ant.parser;

import java.io.FileInputStream;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.beans.StringBean;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.utc.ia04.antsontheweb.knowledgeBase.KnowledgeBaseAgent;
import org.utc.ia04.antsontheweb.main.DefaultClient;
import org.utc.ia04.antsontheweb.utilities.KnowledgeBaseRequest;
import org.utc.ia04.antsontheweb.utilities.Requete;

import jade.core.AID;
import jade.core.Agent;
import jade.lang.acl.ACLMessage;

public class AntParserAgent extends Agent
{
	//serialVersionUID
	private static final long serialVersionUID = 8319871014189198509L;
	
	//attributes
	private AID pathFinderAID;
	private AID antAID;
	private int websiteType;
	private Set<String> interestingWords;
	private Set<URL> links;
	private boolean myAntIsDead = false;
	
	public void setup()
	{
		Object[] args = getArguments();
		pathFinderAID =new AID( args[1].toString(),true);// plutot que true il faudrait mettre un isGUID sur l'argument passé au constructeur
		antAID =new AID( args[0].toString(),true);
		interestingWords = getInterestingKeyWords();
		links = new HashSet<URL>();
		addBehaviour(new AntParserBhv());
	}
	
	//methods
	
	// accessors
	AID getAntAID(){return antAID;}
	AID getPathFinderAID(){return pathFinderAID;}
	public void setMyAntIsDead() { myAntIsDead = true; }
	
	
	Set<URL> getLinks()
	{
		return links;
	}

	void setLinks(String parHtmlContent)
	{
		try
		{
			//System.out.println(parHtmlContent);
			
			links.clear();//efface les lines des autres site visitees
			Page page = new Page(parHtmlContent);
			Lexer lexer = new Lexer(page);
			Parser parser = new Parser(lexer);
			
			NodeFilter filter = new NodeClassFilter(LinkTag.class);
			NodeList list = parser.parse(filter);
			
			for (int i = 0; i < list.size(); i++)
			{
				LinkTag linkTag = (LinkTag)list.elementAt(i);
				
				/*TODO AMELIORER LA REGEX QUI NETTOIE LES CARACTERES DE MERDE */
				//System.out.println("AVANT: "+ linkTag.extractLink());
				String url = linkTag.extractLink().replaceAll("\\\\\"", "");// 4 \ sont traduit en un seul...
				//System.out.println("APRES: "+ url);
				if ((url.endsWith("org")||url.endsWith("com")||url.endsWith("fr")||url.endsWith("/")||url.endsWith("html")||url.endsWith("htm")||url.endsWith("php"))&&((!url.startsWith("mailto"))))
				{
					try
					{
						URL tmp = new URL(url);
						links.add(tmp);
					}
					catch(Exception e)
					{
						//System.out.println("This URL was not concidered : " + url);
					}
					//System.out.println(this.getName()+" : has this links: "+ this.getLinks());
				}
			}
		}
		catch(Exception e)
		{
			System.out.println("Erreur parsing html : " + e.toString());
		}
	}
	
	KnowledgeBaseRequest sortHtmlContent(String parHtmlContent)
	{
		try
		{
			Page page = new Page(parHtmlContent);
			Lexer lexer = new Lexer(page);
			Parser parser = new Parser(lexer);
			
			StringBean sb = new StringBean();
			sb.setCollapse(true);
			
			parser.visitAllNodesWith(sb);
			String result = sb.getStrings();
			String[] decoupe = result.split("\\p{Blank}");
			HashSet<String> keywords = new HashSet<String>();
			
			for(int i = 0; i < decoupe.length; ++i){
				if(interestingWords.contains(decoupe[i])){
					keywords.add(decoupe[i]);
				}
			}
			
			return new KnowledgeBaseRequest(websiteType, keywords);
		}
		catch(Exception e)
		{
			System.out.println("Erreur parsing html : " + e.toString());
			return null;
		}
	}
	
	 /*
	   * Reads data from a given file into a Vector
	   */

	  public HashSet<String> getInterestingKeyWords()
	  {
		  try
		  {
			  //A) Lecture du fichier
			  FileInputStream fileStream = new FileInputStream(DefaultClient.LOCAL_FOLDER + KnowledgeBaseAgent.USED_KEYWORD_FILE);
			  
			  //Deserialisation
			  ObjectMapper m = new ObjectMapper();
			  HashSet<String> resultat = m.readValue(fileStream, new TypeReference<HashSet<String>>(){});
			  return resultat;
		  }
		  catch(Exception e)
		  {
			  System.out.println("Erreur dans getInterestingKeyWords : " + e.toString());
			  return new HashSet<String>();
		  }
	  }
	  
	  public void takeDown()
	  {
		  if(!myAntIsDead)
		  {
			  ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
			  message.addReceiver(getAntAID());
			  Requete req = new Requete(Requete.AC_DEATH, null);
			  message.setContent(req.serialize());
			  send(message);
		  }
	  }
}
